package fei.yuzhi.com.loveflowers.fragment.firstpagefragment.detail.evaluate;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.Toast;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import fei.yuzhi.com.loveflowers.R;
import fei.yuzhi.com.loveflowers.bean.evaluatebean.CommentsListBean;
import fei.yuzhi.com.loveflowers.bean.evaluatebean.EvaluateBean;
import fei.yuzhi.com.loveflowers.utils.MyListView;

/**
 * Created by My on 2016/10/8.
 */
public class EvaluateFragment extends Fragment implements EvaluateContract.IEvaluateView {
    @BindView(R.id.evaluate_lv)
    MyListView evaluateLv;
    @BindView(R.id.evaluate_all)
    RadioButton evaluateAll;
    @BindView(R.id.evaluate_good)
    RadioButton evaluateGood;
    @BindView(R.id.evaluate_normal)
    RadioButton evaluateNormal;
    @BindView(R.id.evaluate_bad)
    RadioButton evaluateBad;
    @BindView(R.id.evaluate_have_image)
    RadioButton evaluateHaveImage;
    private EvaluatePresenter presenter;
    private Handler handler = new Handler();
    private List<CommentsListBean> commentsListBeen;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
    Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout
                .fragment_firstpage_detail_evaluate, null);
        Intent intent = getActivity().getIntent();
        String data = intent.getStringExtra("data");
        presenter = new EvaluatePresenter(EvaluateFragment.this);
        presenter.getEvaluateBean(data);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void getEvaluateBeanSuccess(final EvaluateBean evaluateBean) {
        handler.post(new Runnable() {
            @Override
            public void run() {
                commentsListBeen = evaluateBean.getDatas()
                        .getComments_list();
                evaluateAll.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (commentsListBeen.size() != 0) {
                            EvaluateListAdapter adapter = new EvaluateListAdapter(getActivity(),
                                    commentsListBeen,evaluateAll);
                            evaluateLv.setAdapter(adapter);
                        }
                    }
                });
                evaluateBad.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (commentsListBeen.size() != 0) {
                            EvaluateListAdapter adapter = new EvaluateListAdapter(getActivity(),
                                    commentsListBeen,evaluateBad);
                            evaluateLv.setAdapter(adapter);
                        }
                    }
                });
            }
        });
    }

    @Override
    public void getEvaluateBeanFail(String msg) {
        Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
    }
}
